package com.base.cn.platform.os.controller.count;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.course.count.CourseCountService;
import com.base.cn.platform.os.service.exam.count.ExamCountService;
import com.base.cn.platform.os.service.manage.order.count.OrderCountService;
import com.base.cn.platform.os.service.manage.user.count.UserCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 基础统计Controller
 *
 * @author s.li
 * @create 2018-08-23-21:01
 */
@Controller
@RequestMapping("/admin/count")
public class BaseStatisticsController extends AdminBaseController {

    @Autowired
    private UserCountService userCountService;
    @Autowired
    private CourseCountService courseCountService;
    @Autowired
    private OrderCountService orderCountService;
    @Autowired
    private ExamCountService examCountService;

    /**
     * 初始化基础数据统计页面
     * @return
     */
    @RequestMapping("/queryCount")
    public String queryCount(){
        return resultPage("/count/base-statistics");
    }


    /**
     * 获取基础数据统计
     * @return
     */
    @RequestMapping("/getCountNumData")
    @ResponseBody
    public Map<String,Object> getCountNumData(){
        //注册用户总数、付费用户总数
        Map<String,Integer> userCountMap = userCountService.findUserCount();
        //课程总数、总直播课节
        Map<String, Integer> courseNumMap = courseCountService.queryCourseCountNum();
        //试题总数、试卷总数
//        Map<String,Integer> examCountNumMap = examCountService.queryExamCountNum();
        Map<String,Integer> examCountNumMap = new HashMap<>();
        Map<String,Map<String,Integer>> dataMap = new HashMap<>();
        dataMap.put("userCountMap",userCountMap);
        dataMap.put("courseNumMap",courseNumMap);
        dataMap.put("examCountNumMap",examCountNumMap);
        return ResultUtil.SUCCESS(dataMap);
    }

    /**
     * (今日网站注册量曲线)查询某一天平台用户24小时内每个小时的注册量
     * @return UserRegDayNum
     */
    @RequestMapping("/queryUserRegDayTimeNum")
    @ResponseBody
    public Map<String,Object> queryUserRegDayTimeNum(@RequestParam("date") String date){
        if(StringUtils.isEmpty(date)){
            date =DateUtils.format(new Date(),"yyyy-MM-dd");
        }
        List<Map<String,Integer>> userRegDayTimeNumList = userCountService.queryUserRegDayTimeNum(date);
        Map<String,Object> resultMap = ResultUtil.SUCCESS(userRegDayTimeNumList);
        int max = 0;
        if(ObjectUtils.isNotEmpty(userRegDayTimeNumList)){
            IntSummaryStatistics stats = userRegDayTimeNumList.stream().mapToInt((x) -> x.get("regNum")).summaryStatistics();
            max = stats.getMax();
        }
        resultMap.put("max",max);
        return resultMap;
    }


    /**
     * (今日网站订单量曲线)获取当天订单量
     * @return 统计数据对象
     */
    @RequestMapping("/queryOrderNowDayNum")
    @ResponseBody
    public Map<String,Object> queryOrderNowDayNum(@RequestParam("date") String date){
        if(StringUtils.isEmpty(date)){
            date =DateUtils.format(new Date(),"yyyy-MM-dd");
        }
        List<Map<String,Integer>> orderNowDayNumList = orderCountService.queryOrderNowDayNum(date);
        Map<String,Object> resultMap = ResultUtil.SUCCESS(orderNowDayNumList);
        int max = 0;
        if(ObjectUtils.isNotEmpty(orderNowDayNumList)){
            IntSummaryStatistics sumNum = orderNowDayNumList.stream().mapToInt((x) -> x.get("sumNum")).summaryStatistics();
            max = sumNum.getMax();
        }
            resultMap.put("max",max);
        return resultMap;
    }

    /**
     * 查询用户注册地理布局
     * @return 用户注册布局列表
     */
    @RequestMapping("/queryUserRegLayoutNum")
    @ResponseBody
    public Map<String,Object> queryUserRegLayoutNum(){
        List<Map<String,Object>> userRegLayoutNumList = userCountService.queryUserRegLayoutNum();
        Map<String,Object> resultMap = ResultUtil.SUCCESS(userRegLayoutNumList);
        int max = 0;
        if(ObjectUtils.isNotEmpty(userRegLayoutNumList)){
            IntSummaryStatistics initNum = userRegLayoutNumList.stream().mapToInt((x) -> Integer.parseInt(x.get("userNum").toString())).summaryStatistics();
            max = initNum.getMax();
        }
        resultMap.put("max",max);
        return resultMap;
    }

    /**
     * 用户登录设备统计量
     * @return 统计对象
     */
    @RequestMapping("/queryUserLoginDeviceNum")
    @ResponseBody
    public Map<String,Object> queryUserLoginDeviceNum(){
        Map<String,Object> userLoginDeviceNumMap = userCountService.queryUserLoginDeviceNum();
        return ResultUtil.SUCCESS(userLoginDeviceNumMap);
    }


    /**
     * (网站注册量曲线)查询某一时间段的注册量
     * @return UserRegDayNum
     */
    @RequestMapping("/queryUserDayTimeNum")
    @ResponseBody
    public Map<String,Object> queryUserDayTimeNum(@RequestBody Map<String,Object> map) {
        List<Map<String, Object>> userRegDayTimeNumList = userCountService.queryUserDayTimeNum(map);
        Map<String, Object> resultMap = ResultUtil.SUCCESS(userRegDayTimeNumList);
        int max = 0;
        if (ObjectUtils.isNotEmpty(userRegDayTimeNumList)) {
            IntSummaryStatistics stats = userRegDayTimeNumList.stream().mapToInt((x) -> Integer.valueOf(x.get("regNum").toString())).summaryStatistics();
            max = stats.getMax();
        }
        resultMap.put("max", max);
        return resultMap;
    }


    /**
     * (网站订单量曲线)获取某时间段的订单曲线
     * @return 统计数据对象
     */
    @RequestMapping("/queryOrderDayNum")
    @ResponseBody
    public Map<String,Object> queryOrderDayNum(@RequestBody Map<String,Object> map){
        List<Map<String,Object>> orderNowDayNumList = orderCountService.queryOrderDayNum(map);
        Map<String,Object> resultMap = ResultUtil.SUCCESS(orderNowDayNumList);
        int max = 0;
        if(ObjectUtils.isNotEmpty(orderNowDayNumList)){
            IntSummaryStatistics sumNum = orderNowDayNumList.stream().mapToInt((x) -> Integer.valueOf(x.get("sumNum").toString())).summaryStatistics();
            max = sumNum.getMax();
        }
        resultMap.put("max",max);
        return resultMap;
    }

    /**
     * 获取基础数据统计
     * @return
     */
    @RequestMapping("/getConversionRate")
    @ResponseBody
    public Map<String,Object> getConversionRate(@RequestBody Map map){
        Map<String,Object> dataMap = new HashMap<>();
        //日活
        dataMap.put("activeDUA", userCountService.findActiveDUA(map));
        //购买转化率
        dataMap.put("orderConversionRate",userCountService.findOrderConversionRate(map));
        //注册转化率
        dataMap.put("registerConversionRate",userCountService.findRegisterConversionRate(map));
        //7日转化
        dataMap.put("sevenDayConversion",userCountService.findSevenDayConversion());
        //30日转化
        dataMap.put("thirtyDayConversion",userCountService.findThirtyDayConversion());
        return ResultUtil.SUCCESS(dataMap);
    }
}
